const path = require("path");
const webpack = require('webpack');
const node_modules_dir = path.resolve(__dirname, 'node_modules');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

function setConfig(setting) {
    const _DEV_ = setting._DEV_
    return  {
        watch: _DEV_,
        devtool: _DEV_ ? "#cheap-module-source-map" : "#hidden-source-map",
        entry: setting.entry,
        output: {
            path: path.resolve(__dirname, 'dist/'),
            filename: "[name].bundle.js"
        },
        resolve: {
            extensions: ['.js', '.jsx', '.scss','.less']
        },
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    use: _DEV_ ? ['react-hot-loader', 'babel-loader'] : ['babel-loader'],
                    exclude: /node_modules/,
                    include: __dirname
                },
                {
                    test: /\.css$/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: [{
                            loader: 'css-loader',
                            options: {sourceMap: _DEV_}
                        }, 'postcss-loader']
                    })
                },
                {
                    test: /\.scss$/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: [{
                            loader: 'css-loader',
                            options: {sourceMap: _DEV_}
                        }, 'postcss-loader', 'sass-loader']
                    })
                },
                {
                    test: /\.less/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: [{
                            loader: 'css-loader',
                            options: {sourceMap: _DEV_}
                        }, 'postcss-loader', 'less-loader']
                    })
                },
                {
                    test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|json|mp4)$/,
                    use: {
                        loader: 'url-loader', options: {
                            limit: 8192
                        }
                    }

                }
            ]
        },
        plugins: [
            new ExtractTextPlugin('[name].bundle.css'),
            ...setting.plugins
        ]

    }

}

module.exports = setConfig